package cn.com.ddapp.ddm.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.bean.SlidingSmallBean;

import static cn.com.ddapp.ddm.R.id.slidingbig_name;


public class SlidingGoodSmallAdpter extends BaseAdapter {
    private Context mContext;
    private List<SlidingSmallBean.brandList> mLists ;
    private LayoutInflater layoutInflater;
    HashMap<Integer, Boolean> state = new HashMap<Integer, Boolean>();

    private List<CheckBox> checkBoxes = new ArrayList<>();

    public SlidingGoodSmallAdpter(Context context, List<SlidingSmallBean.brandList> lists) {
        mContext = context;
        mLists = lists;
        layoutInflater=LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return mLists != null ? mLists.size():0;
    }

    @Override
    public Object getItem(int i) {
        return mLists.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    public void addView(List<SlidingSmallBean.brandList> ll){
        if(ll.size()<=0){
            return;
        }else{
            mLists.addAll(ll);
        }
    }

    public void checkAll(boolean checked) {
        for (int i = 0 ; i < mLists.size(); i++) {
            SlidingSmallBean.brandList beana = mLists.get(i);
            beana.rb_select_cart=checked;
        }
        notifyDataSetChanged(); // 刷新列表
    }

    public void clear(){
        for (int i = 0; i < mLists.size(); i++) {
            mLists.get(i).setCheck(false);
        }
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder=null;
//        if(convertView == null){
            convertView= layoutInflater.inflate(R.layout.sliding_goodname_item,null);
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
            AutoUtils.auto(convertView);

//        }else{
//            holder= (ViewHolder) convertView.getTag();
//        }
        final List<String> proList=new ArrayList<String>();
        holder.slidingbig_name.setText(mLists.get(position).getName());
        checkBoxes.add(holder.slidingbig_name);
//        holder.slidingbig_name.setId(mLists.get(position).getId());
        holder.slidingbig_name.setTag(mLists.get(position).getId());
//        holder.slidingbig_name.setChecked(mLists.get(position).isCheck());
        final ViewHolder finalHolder = holder;
        holder.slidingbig_name.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isCheck) {

//                for(CheckBox checkBox : checkBoxes){
//                    if(checkBox.getText().equals(finalHolder.slidingbig_name.getText())){
//                        checkBox.setChecked(false);
//                    }
//                }

                int tag = (Integer) (compoundButton.findViewById(compoundButton.getId()).getTag());
                int bId;
                if(isCheck){
                    bId=tag;
                    state.put(tag,isCheck);
                }else{
                    bId=0;
                    state.remove(tag);
                }
                ;
                Log.i("aaa","--p-"+state);
                Intent intent=new Intent();
                intent.putExtra("pinpaiId",bId);
                intent.setAction("pinpaiId");
                mContext.sendBroadcast(intent);
            }
        });
        return convertView;
    }

    static class ViewHolder{
        @BindView(R.id.slidingsmall_name)
        CheckBox slidingbig_name;


        public ViewHolder(View view){
            ButterKnife.bind(this,view);
        }
    }
}
